package org.alis.rpc.common.paap;

import org.alis.rpc.common.paap.cache.ConfigCacheService;
import org.alis.rpc.common.paap.cache.bean.ConfigValue;
import org.alis.rpc.common.paap.util.AppConfigClientUtil;

import java.util.List;
import java.util.Map;

/**
 * @author lc
 * @version v1.0.0
 * @date 2020/8/28 11:22
 * @className Configurator.class
 */
public class Configurator {
    /**
     * 客户端缓存服务
     */
    private ConfigCacheService configCacheService;

    public void setConfigCacheService(ConfigCacheService configCacheService) {
        this.configCacheService = configCacheService;
    }

    /**
     * 获取某个配置信息的默认值
     *
     * @param key 配置项的key
     * @return String 配置项的值
     */
    public String getSysConfig(String key) {
        return configCacheService.getConfigValue(key);
    }

    public String addConfigValue(String appId, List<ConfigValue> configvalues){
        String rootUrl = configCacheService.getAppconfigServerUrl();
        return AppConfigClientUtil.addConfigValue(appId,rootUrl, configvalues);
    }

    /**
     * Get specific corp value, if not exists. <code>null</code> is returned.
     *
     * @param key    configuration key.
     * @param corpId specific corp.
     * @return Corp config value.
     */
    public String getSysConfigWithCorp(String key, String corpId) {
        return configCacheService.getConfigValueWithCorp(key, corpId);
    }

    /**
     * 获取某个配置信息的配置值
     *
     * @param key    String 配置信息的key
     * @param corpId String 单位标识
     * @return 单位配置项的value, String类型
     */
    public String getSysConfig(String key, String corpId) {
        return configCacheService.getConfigValueWithDefault(key, corpId);
    }

    public Map<String, String> getAllSysConfig(String key) {
        return configCacheService.getConfigValueMap(key);
    }

    /**
     * 直接去配置服务端获取配置信息，不是在客户端缓存中取
     *
     * @param appId  应用分组id
     * @param key    配置信息的key
     * @param corpId 单位标识
     * @return
     */
    public String getSysConfig(String appId, String key, String corpId) {
        String rootUrl = configCacheService.getAppconfigServerUrl();
        return AppConfigClientUtil.getConfig(rootUrl, appId, key, corpId);
    }
}
